if (!jsx3.lang.Class.forName("tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.MonitoringConfig")) {

    jsx3.lang.Package.definePackage("tibco.admin.monitormanage.uicomponents.misc.monitoringConfig", function (MonitoringConfig) {
    });

    jsx3.lang.Class.defineClass("tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.MonitoringConfig",
            jsx3.lang.Object,
            [tibco.admin.enterpriseAssets.servers.LeftNavInterface],
            function (plugin) {

                plugin.ONEDIT = "tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.EDIT";
                plugin.ONCANCEL = "tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.CANCEL";
                plugin.ONSAVE = "tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.SAVE";
                plugin.ONMONITORYES = "tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.MONITORYES";
                plugin.ONMONITORNO = "tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.MONITORNO";
                plugin.ONPICKRESOURCE = "tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.PICKRESOURCE";
                plugin.ONPICKNODE = "tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.PICKNODE";

                plugin.instance = null

                plugin.serviceEndpoint = "/" + tibco.admin.util.ServiceHelper.getAdminWebapp() + "/services/monitoringconfig";

                plugin.prototype.mainComponent = null;
                plugin.prototype.editMode = false;
                
               	plugin.DEFAULT_JMS_CONFIG;
               	plugin.DEFAULT_NODE;

                plugin.prototype.init = function() {                
                    tibco.admin.Main.loadResource("tibco/admin/monitormanage/uicomponents/misc/monitoringConfig/jss/jss.xml",
                            this.getNamespace(), "jss22H", "ljss", false);
                    if (!tibco.admin.enterpriseAssets.shdResDefs.ShdResDefPicker) {
                        tibco.admin.Main.loadClass("tibco/admin/enterpriseAssets/shdResDefs/ShdResDefPicker.js",
                                this.getNamespace());
                    }
                    if(!tibco.admin || !tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.misc || !tibco.admin.monitormanage.uicomponents.misc.monitoringConfig || !tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.CustomButton)
                    { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/misc/monitoringConfig/CustomButton.js", this.getNamespace()); }
                    this.jsxsuper();
                }

                plugin.prototype.getNamespace = function() {
                    return "tibco.admin";
                }

                plugin.prototype.destroyPlugin = function() {
                }

                plugin.getInstance = function(args) {
                    if (!this.instance) {
                        this.instance = new tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.MonitoringConfig();
                    }
                    return this.instance;
                }
		
		plugin.prototype.getComponentPath = function() {
			return "tibco/admin/monitormanage/uicomponents/misc/monitoringConfig/component.xml";
	        }

                plugin.prototype.loadComponent = function(parent) {
                	this.editMode = false;
                    tibco.admin.Main.loadUIResourceAsync(parent, "tibco.admin",
                            "tibco/admin/monitormanage/uicomponents/misc/monitoringConfig/components/monitoringConfig.xml",
                            this, this.initialize);
                }

                plugin.prototype.getLeftText = function() {
                    return tibco.admin.Main.getServer().getDynamicProperty("@leftNavTabText_22H");
                }

                plugin.prototype.initialize  = function(objEvent) {
                    if(objEvent.status == "SUCCESS") {
                    	var server = tibco.admin.Main.getServer();
                       	plugin.DEFAULT_JMS_CONFIG = server.getDynamicProperty("@defaultJmsConfig_22H");
                       	plugin.DEFAULT_NODE = server.getDynamicProperty("@defaultNodeConfig_22H");
                    
                        this.mainComponent = objEvent.obj;
                        this.mainComponent.getDescendantOfName("btnCancelDetails_22H").subscribe(plugin.ONCANCEL, this,
                                this.onCancel);
                        this.mainComponent.getDescendantOfName("btnSaveDetails_22H").subscribe(plugin.ONSAVE,
                                this, this.onSave);
                        this.mainComponent.getDescendantOfName("btnEditDetails_22H").subscribe(plugin.ONEDIT, this,
                                this.onEdit);
                        this.mainComponent.getDescendantOfName("radEnableYes_22H").subscribe(plugin.ONMONITORYES,
                                this, this.enableMonitoring);
                        this.mainComponent.getDescendantOfName("radEnableNo_22H").subscribe(plugin.ONMONITORNO,
                                this, this.disableMonitoring);
			var jmsCfgButton = this.mainComponent.getDescendantOfName("btnJmsConfig_22H");
			jmsCfgButton.setText(plugin.DEFAULT_JMS_CONFIG);
                        jmsCfgButton.subscribe(plugin.ONPICKRESOURCE,
                                this, this.pickConfigResource);
                        jmsCfgButton.subscribe(tibco.admin.monitormanage.uicomponents.misc.monitoringConfig.CustomButton.ONSETTEXT, this, this.configResourceSelected);
                        var nodeCfgButton = this.mainComponent.getDescendantOfName("selectNode_22H");
                        nodeCfgButton.subscribe(plugin.ONPICKNODE, this, this.pickNode);
                        this.callGetMonitoringConfiguration();
                    } else {
                        tibco.admin.util.Log.ENVIRONMENT.error(tibco.admin.Main.getServer().getDynamicProperty("@errorMsgInitialize_22H", objEvent.message));
                    }
                }

                plugin.prototype.onEdit = function() {
                	this.beginEditMode();
                	this.callGetAllAdminNodes();
                    this.editMode = true;
                    this.switchMode();
                    this.setEditable(true);
                    this.handleSaveButtonEnable(null);
                }

                plugin.prototype.onCancel = function(objEvent) {
                    this.editMode = false;
                    this.endEditMode();
                    /*
					var server = tibco.admin.Main.getServer();
					var yesLabel = server.getDynamicProperty("@MM@general@monitorConfigYesLabel");
                    var enabled = (this.mainComponent.getDescendantOfName("lblcEnable_22H").getText() == yesLabel) ? "true" : "false";
                    this.mainComponent.getDescendantOfName("radEnableYes_22H").setGroupValue(enabled, true);
                    if (enabled == "true") {
                        this.mainComponent.getDescendantOfName("btnJmsConfig_22H").setText(this.mainComponent.getDescendantOfName("lblcJmsConfig_22H").getText(), true);
                        this.mainComponent.getDescendantOfName("selectNode_22H").setValue(this.mainComponent.getDescendantOfName("lblcNode_22H").getText(), true);
                        this.showConfigDetails(true);
                    } else {
                        this.mainComponent.getDescendantOfName("btnJmsConfig_22H").setText(plugin.DEFAULT_JMS_CONFIG, true);
                        this.mainComponent.getDescendantOfName("selectNode_22H").setValue(plugin.DEFAULT_NODE, true);
                        this.showConfigDetails(false);
                    }
                    */
                    this.switchMode();
                    /*
                    if (this.mainComponent.getDescendantOfName("lblcEnable_22H").getText() == yesLabel) {
                        this.showConfigDetails(true);
                    } else {
                        this.showConfigDetails(false);
                    }
                    */
                    this.callGetMonitoringConfiguration();
//                    this.setEditable(true);
                }

                plugin.prototype.onSave = function(objEvent) {
                    this.callSetMonitoringConfiguration(objEvent);
                    this.endEditMode();
                }
                
                plugin.prototype.getConfirmTitle = function() {
                	return tibco.admin.Main.getServer().getDynamicProperty("@lblDiscardEditsTitle_22H");
                }
                
                plugin.prototype.getConfirmText = function() {
            		return tibco.admin.Main.getServer().getDynamicProperty("@lblDiscardEdits_22H");
        		}

                plugin.prototype.switchMode = function() {
                    if (!this.editMode
                            && (this.mainComponent.getDescendantOfName("btnCancelDetails_22H").getDisplay()
                            == jsx3.gui.Block.DISPLAYNONE))
                        return;

                    var display = this.editMode ? jsx3.gui.Block.DISPLAYNONE : jsx3.gui.Block.DISPLAYBLOCK;
                    this.mainComponent.getDescendantOfName("btnEditDetails_22H").setDisplay(display, true);
                    this.mainComponent.getDescendantOfName("lblcEnable_22H").setDisplay(display, true);
                    this.mainComponent.getDescendantOfName("lblcJmsConfig_22H").setDisplay(display, true);
                    this.mainComponent.getDescendantOfName("lblcNode_22H").setDisplay(display, true);

                    display = this.editMode ? jsx3.gui.Block.DISPLAYBLOCK : jsx3.gui.Block.DISPLAYNONE;
                    this.mainComponent.getDescendantOfName("btnCancelDetails_22H").setDisplay(display, true);
                    this.mainComponent.getDescendantOfName("btnSaveDetails_22H").setDisplay(display, true);
                    this.mainComponent.getDescendantOfName("blkEnableYesNo_22H").setDisplay(display, true);
                    this.mainComponent.getDescendantOfName("btnJmsConfig_22H").setDisplay(display, true);
                    this.mainComponent.getDescendantOfName("selectNode_22H").setDisplay(display, true);
                }

                plugin.prototype.setEditable = function(state) {
                    var editEnabled = state ? jsx3.gui.Form.STATEENABLED : jsx3.gui.Form.STATEDISABLED;
                    if (this.editMode) {
                        this.mainComponent.getDescendantOfName("radEnableYes_22H").setEnabled(editEnabled, true);
                        this.mainComponent.getDescendantOfName("radEnableNo_22H").setEnabled(editEnabled, true);
                        this.mainComponent.getDescendantOfName("btnJmsConfig_22H").setEnabled(editEnabled, true);
                        this.mainComponent.getDescendantOfName("selectNode_22H").setEnabled(editEnabled, true);
                        if (state) {
                            this.onStartEdit();
                        }
                    } else {
                        this.mainComponent.getDescendantOfName("btnEditDetails_22H").setEnabled(editEnabled, true);
                    }
                }

                plugin.prototype.showConfigDetails = function(state) {
                    this.mainComponent.getDescendantOfName("blkJmsConfig_22H").setDisplay(state ? jsx3.gui.Block.DISPLAYBLOCK : jsx3.gui.Block.DISPLAYNONE, true);
                    this.mainComponent.getDescendantOfName("blkNode_22H").setDisplay(state ? jsx3.gui.Block.DISPLAYBLOCK : jsx3.gui.Block.DISPLAYNONE, true);
                    
                    this.handleSaveButtonEnable(state);
                }

                plugin.prototype.enableMonitoring = function() {
                    this.showConfigDetails(true);
                }
                plugin.prototype.disableMonitoring = function() {
                    var defaultValue = plugin.DEFAULT_JMS_CONFIG;
                    this.mainComponent.getDescendantOfName("btnJmsConfig_22H").setText(defaultValue, true);
                    this.mainComponent.getDescendantOfName("lblcJmsConfig_22H").setText(defaultValue, true);
                    defaultValue = plugin.DEFAULT_NODE;
                    this.mainComponent.getDescendantOfName("selectNode_22H").setValue(defaultValue, true);
                    this.mainComponent.getDescendantOfName("lblcNode_22H").setText(defaultValue, true);
                    this.showConfigDetails(false);
                }

                plugin.prototype.onStartEdit = function(objEvent) {
                    var saveButton = this.mainComponent.getDescendantOfName("btnSaveDetails_22H");
                    if (saveButton.getEnabled() == jsx3.gui.Form.STATEDISABLED) {
                        saveButton.setEnabled(jsx3.gui.Form.STATEENABLED, true);
                    }
                }
                
                plugin.prototype.handleSaveButtonEnable = function(enabled) {
					var monitoringEnabled;
					if(enabled == null)
					{ monitoringEnabled = this.mainComponent.getDescendantOfName("radEnableYes_22H").getGroupValue();}
					else
					{ monitoringEnabled = enabled; }
					var buttonState = jsx3.gui.Form.STATEDISABLED;
                    if(monitoringEnabled+"" == "true")
					{
	                	var jmsConfig = this.mainComponent.getDescendantOfName("btnJmsConfig_22H").getText();
	                	var selectedNode = this.mainComponent.getDescendantOfName("selectNode_22H").getValue();
						if(jmsConfig != plugin.DEFAULT_JMS_CONFIG && selectedNode != plugin.DEFAULT_NODE)
						{ buttonState = jsx3.gui.Form.STATEENABLED; }
					}
					else
					{ buttonState = jsx3.gui.Form.STATEENABLED; }
	                var saveButton = this.mainComponent.getDescendantOfName("btnSaveDetails_22H");
                    if (saveButton.getEnabled() != buttonState) 
                    { saveButton.setEnabled(buttonState, true); }
                }

                plugin.prototype.pickConfigResource = function(event) {
                    var pick = this.mainComponent.getDescendantOfName("btnJmsConfig_22H").getText();
                    if (pick == plugin.DEFAULT_JMS_CONFIG) {
                        pick = null;
                    }
                    tibco.admin.enterpriseAssets.shdResDefs.ShdResDefPicker.pickChoice(event.source, "JMS", pick);
                }

				plugin.prototype.pickNode = function(event) {
					this.handleSaveButtonEnable(null);
				}
				
				plugin.prototype.configResourceSelected = function(event) {
					this.handleSaveButtonEnable(null);
				}

                // WSDL operations
                // WSDL operation - getMonitoringConfiguration
                plugin.prototype.callGetMonitoringConfiguration = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/monitormanage/uicomponents/misc/monitoringConfig/rules/getMonitoringConfiguration.xml",
                            "getMonCfg", tibco.admin.Main.getServer().getDynamicProperty("@getMonitoringConfigMsg_22H"));
                    objService.setEndpointURL(plugin.serviceEndpoint);

                    //subscribe and call
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onGetMonitoringConfigurationSuccess);
                    objService.doCall();
                }

                plugin.prototype.onGetMonitoringConfigurationSuccess = function(objEvent) {
					var server = tibco.admin.Main.getServer();
                    var response = tibco.admin.Main.getServer().Cache.getDocument("xmlCacheIDGetMonitoringConfigurationResponse_22H").getRecordNode("jsxroot");
                    var enabled = response.getAttribute("enableMonitoring");
					var yesLabel = server.getDynamicProperty("@MM@general@monitorConfigYesLabel");
					var noLabel = server.getDynamicProperty("@MM@general@monitorConfigNoLabel");
                    this.mainComponent.getDescendantOfName("lblcEnable_22H").setText((enabled == "true") ? yesLabel : noLabel, true);
                    this.mainComponent.getDescendantOfName("radEnableYes_22H").setGroupValue(enabled, true);
                    if (enabled == "true") {
                        var value = response.getAttribute("jmsResourceName");
                        if (!value) {
                            value = plugin.DEFAULT_JMS_CONFIG;
                        }
                        this.mainComponent.getDescendantOfName("btnJmsConfig_22H").setText(value, true);
                        this.mainComponent.getDescendantOfName("lblcJmsConfig_22H").setText(value, true);
                        
                        // handle the node configuration field
                        var adminNodeName = response.getAttribute("adminNodeName");
                        if(!adminNodeName)
                        { adminNodeName = plugin.DEFAULT_NODE; }
                        
                        this.mainComponent.getDescendantOfName("lblcNode_22H").setText(adminNodeName, true);
                        this.mainComponent.getDescendantOfName("selectNode_22H").setValue(adminNodeName, true);
                        
                        this.showConfigDetails(true);
                    } else {
                        this.showConfigDetails(false);
                    }

                    this.setEditable(true);
                };

                // WSDL operation - setMonitoringConfiguration
                plugin.prototype.callSetMonitoringConfiguration = function() {
                    this.setEditable(false);

                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/monitormanage/uicomponents/misc/monitoringConfig/rules/setMonitoringConfiguration.xml",
                            "setMonCfg", tibco.admin.Main.getServer().getDynamicProperty("@setMonitoringConfigMsg_22H"));
                    objService.setEndpointURL(plugin.serviceEndpoint);

                    objService.enableMonitoring = this.mainComponent.getDescendantOfName("radEnableYes_22H").getGroupValue();
                    if (objService.enableMonitoring == "true") {
                        objService.jmsResourceName = this.mainComponent.getDescendantOfName("btnJmsConfig_22H").getText();
                        objService.adminNodeName = this.mainComponent.getDescendantOfName("selectNode_22H").getValue();
                    }

                    //subscribe and call
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onSetMonitoringConfigurationSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onSetMonitoringConfigurationErrorOrInvalid);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onSetMonitoringConfigurationErrorOrInvalid);
                    objService.doCall();
                }

                plugin.prototype.onSetMonitoringConfigurationSuccess = function(objEvent) {
					var server = tibco.admin.Main.getServer();
                    var enabled = this.mainComponent.getDescendantOfName("radEnableYes_22H").getGroupValue();
					var yesLabel = server.getDynamicProperty("@MM@general@monitorConfigYesLabel");
					var noLabel = server.getDynamicProperty("@MM@general@monitorConfigNoLabel");
                    this.mainComponent.getDescendantOfName("lblcEnable_22H").setText((enabled == "true") ? yesLabel : noLabel, true);
                    if (enabled == "true") {
                        var value = this.mainComponent.getDescendantOfName("btnJmsConfig_22H").getText();
                        this.mainComponent.getDescendantOfName("lblcJmsConfig_22H").setText(value, true);
                        var nodeName = this.mainComponent.getDescendantOfName("selectNode_22H").getValue();
                        this.mainComponent.getDescendantOfName("lblcNode_22H").setText(nodeName, true);
                        this.showConfigDetails(true);
                    } else {
                        this.mainComponent.getDescendantOfName("lblcJmsConfig_22H").setText(plugin.DEFAULT_JMS_CONFIG, true);
                        this.mainComponent.getDescendantOfName("lblcNode_22H").setText(plugin.DEFAULT_NODE, true);
                        this.showConfigDetails(false);
                    }
                    this.editMode = false;
                    this.switchMode();
                }

                plugin.prototype.onSetMonitoringConfigurationErrorOrInvalid = function(objEvent) {
                    this.setEditable(true);
                }
                
                //call this method to begin the service call (eg.service.callgetAllAdminNodes();)
    			plugin.prototype.callGetAllAdminNodes = function() {
    				var objService = tibco.admin.Main.loadService(this.getNamespace(),
                            "tibco/admin/monitormanage/uicomponents/misc/monitoringConfig/rules/getAllAdminNodes.xml",
                            "getAllAdminNodes", tibco.admin.Main.getServer().getDynamicProperty("@getAllAdminNodesMsg_22H"));
		    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getAdminConfigurationEndpointURL());
                    
                    objService.monitoringConfigObj = this;
                    
                    objService.selectedAdminNodeName = this.mainComponent.getDescendantOfName("lblcNode_22H").getText();

      				//subscribe and call
			      	objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetAllAdminNodesSuccess);
			      	objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetAllAdminNodesError);
			      	objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetAllAdminNodesInvalid);
			      	objService.doCall();
    			};

    			plugin.prototype.ongetAllAdminNodesSuccess = function(objEvent) {
      				//var responseXML = objEvent.target.getInboundDocument();
      				//jsx3.log("Success","The service call was successful.");
      				
      				// select the appropriate admin node if necessary
      				var selectedAdminNodeName = objEvent.target.selectedAdminNodeName;
      				if(selectedAdminNodeName != plugin.DEFAULT_NODE)
      				{
      					var selectNodeField = objEvent.target.monitoringConfigObj.mainComponent.getDescendantOfName("selectNode_22H");
      					selectNodeField.setValue(selectedAdminNodeName);
      				}
      				
    			};

    			plugin.prototype.ongetAllAdminNodesError = function(objEvent) {
      				var myStatus = objEvent.target.getRequest().getStatus();
      				jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    			};

    			plugin.prototype.ongetAllAdminNodesInvalid = function(objEvent) {
      				jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    			};

            });
}